package widok;

import java.awt.FlowLayout;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.Model;

public class Widok extends JFrame {

	private static final String WARTOSC_POCZATKOWA = "1";
	
	private JTextField inuput = new JTextField(5);
	private JTextField total = new JTextField(20);
	private JButton mnozenie = new JButton("Mnóż");
	private JButton reset = new JButton("Czyść");
	
	private Model model;
	
	public Widok(Model model) {
		this.model = model;
		this.model.setValue(WARTOSC_POCZATKOWA);
		
		total.setText(WARTOSC_POCZATKOWA);
		total.setEditable(false);
	
		JPanel content = new JPanel();
		content.setLayout(new FlowLayout());
		content.add(new JLabel("Input"));
		content.add(inuput);
		content.add(mnozenie);
		content.add(new JLabel("Total"));
		content.add(total);
		content.add(reset);
		
		this.setContentPane(content);
		this.pack();
		this.setTitle("Implemntacja wzorca MVC");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void reset() {
		total.setText(WARTOSC_POCZATKOWA);
	}
	
	public String getUserInput() {
		return inuput.getText();
	}
	
	public void setTotal(String newTotal) {
		total.setText(newTotal);
	}
	
	public void showError(String errorMsg) {
		JOptionPane.showMessageDialog(this, errorMsg);
	}
	
	public void addMultiplyListener(ActionListener mal) {
		mnozenie.addActionListener(mal);
	}
	
	public void addClearlistener(ActionListener cal) {
		reset.addActionListener(cal);
	}
}
